package io.aircraft.modules.api.controller;

import io.aircraft.common.utils.ApiResult;
import io.aircraft.common.utils.ProcResult;
import io.aircraft.common.validator.ValidatorUtils;
import io.aircraft.modules.api.dto.NoticeByWcsDTO;
import io.aircraft.modules.api.service.ApiTaskService;
import io.aircraft.modules.wms.dto.CallbackRecordDTO;
import io.aircraft.modules.wms.service.CallbackRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalTime;

@RestController
@RequestMapping("/api")
public class ApiTaskController {
    @Autowired
    private ApiTaskService apiTaskService;
    @Autowired
    private CallbackRecordService callbackService;

    /**
     * 连接测试
     */
    @GetMapping("ping")
    public ApiResult ping() {
        return new ApiResult().success("当前时间:" + LocalTime.now());
    }

    @PostMapping("wcsCallback")
    public ApiResult wcsCallback(@RequestBody CallbackRecordDTO dto) {
        //对接口数据进行格式及内容校验
        try {
            ValidatorUtils.validateEntity(dto);
            //保存记录
            callbackService.save(dto);
        } catch (Exception ex) {
            return new ApiResult().feedBack(ex.getMessage(), dto.getRequestPk());
        }
        //逻辑处理
        ProcResult procResult = apiTaskService.callbackByWcs(dto);

        if ("0".equals(procResult.getCode())) {
            return new ApiResult().feedBack(dto.getRequestPk());
        } else {
            return new ApiResult().feedBack(procResult.getCode(), procResult.getMsg(), dto.getRequestPk());
        }
    }

    @PostMapping("wcsNotice")
    public ApiResult wcsNotice(@RequestBody NoticeByWcsDTO dto) {
        //对接口数据进行格式及内容校验
        try {
            ValidatorUtils.validateEntity(dto);
        } catch (Exception ex) {
            return new ApiResult().feedBack(ex.getMessage(), dto.getRequestPk());
        }
        //调用存储过程[proc_noticeByDevice]进行业务处理
        ProcResult procResult = apiTaskService.noticeByWcs(dto);
        if ("0".equals(procResult.getCode())) {
            return new ApiResult().feedBack(dto.getRequestPk());
        } else {
            return new ApiResult().feedBack(procResult.getCode(), procResult.getMsg(), dto.getRequestPk());
        }
    }
}
